﻿Imports System.Web.Configuration
Imports System.Net
Imports System.IO
Imports System.Xml

Namespace MDT_MonitoringService_MVCDemo
    Public Class MDTMonitorEventController
        Inherits System.Web.Mvc.Controller

        Private logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger()

        Function PostEvent(uniqueID As String,
                          computerName As String,
                          messageId As String,
                          severity As String,
                          stepName As String,
                          currentStep As Short?,
                          totalSteps As Short?,
                          id As String,
                          message As String,
                          dartIP As String,
                          dartPort As String,
                          dartTicket As String,
                          vmHost As String,
                          vmName As String) As ActionResult


            If Not String.IsNullOrEmpty(computerName) Then

                ' Forward information to original MDT service if the URL is specified
                Try
                    Dim param As String

                    param = "uniqueID=" & HttpUtility.UrlEncode(uniqueID)
                    param &= "&computerName=" & HttpUtility.UrlEncode(computerName)
                    param &= "&messageID=" & HttpUtility.UrlEncode(messageId)
                    param &= "&severity=" & HttpUtility.UrlEncode(severity)
                    param &= "&stepName=" & HttpUtility.UrlEncode(stepName)
                    param &= "&currentStep=" & HttpUtility.UrlEncode(currentStep)
                    param &= "&totalSteps=" & HttpUtility.UrlEncode(totalSteps)
                    param &= "&id=" & HttpUtility.UrlEncode(id)
                    param &= "&message=" & HttpUtility.UrlEncode(message)
                    param &= "&dartIP=" & HttpUtility.UrlEncode(dartIP)
                    param &= "&dartPort=" & HttpUtility.UrlEncode(dartPort)
                    param &= "&dartTicket=" & HttpUtility.UrlEncode(dartTicket)
                    param &= "&vmHost=" & HttpUtility.UrlEncode(vmHost)
                    param &= "&vmName=" & HttpUtility.UrlEncode(vmName)

                    Dim BaseURL As String = WebConfigurationManager.AppSettings("MDTMonitoringURL") & "/MDTMonitorEvent/PostEvent?"
                    Dim URL As New Uri(BaseURL & param)


                    ' Send data 
                    Dim client As WebClient = New WebClient()
                    Dim data As Stream = client.OpenRead(URL)

                    ' Read uniqueID from MDT
                    Dim reader As StreamReader = New StreamReader(data)
                    Dim MDTXMLResult As XmlDocument = New XmlDocument

                    Dim xmlString As String = reader.ReadToEnd
                    reader.Close()
                    MDTXMLResult.LoadXml(xmlString)
                    If MDTXMLResult.ChildNodes.Count > 0 Then
                        uniqueID = MDTXMLResult.ChildNodes(0).InnerText
                    End If


                Catch exc As Exception
                    Trace.WriteLine("Exception: " & exc.Message)
                End Try


                ' Make sure we have a unique ID for this deployment
                If String.IsNullOrEmpty(uniqueID) Then
                    uniqueID = Guid.NewGuid.ToString
                End If

                ' Log the information for demonstration purposes
                logger.Info("{0} - {1} ({2}/{3}) - {4} ({5}) - {6}", computerName, stepName, currentStep, totalSteps, message, messageId, uniqueID)

            End If

            ' Return the unique ID
            Return New XmlResult(uniqueID)
        End Function


        Function Index() As ActionResult
            Return New ContentResult()
        End Function
    End Class
End Namespace